<template>
	<view class="width600" style="margin:auto;">
		<view class="flex-ju-c mb80 mt50">
			<image mode="widthFix" class="width300" src="/static/loo.png"></image>
		</view>
		<view class="size42 bold mb5 color323232">
			{{texttitle}}
		</view>
		<view class="colorddd size26">
			未注册的手机号登录成功后将自动注册
		</view>
		<view  class="posire height100 flex-a-i mt50">
			<view class="color333 bold pl20 pr20 posiab border-r999 ">
				+86
			</view>
			<input v-model="account" placeholder-style="font-size: 26rpx;" class="bgEDEDED width1000 pl130 height100 ra60 " maxlength="11" placeholder="请输入手机号" />
		</view>
		
		<input v-if="text=='验证码登录'" v-model="password" type="password" placeholder-style="font-size: 26rpx;" class="bgEDEDED width1000 height100 ra60 mt50" style="text-indent: 15px;" placeholder="请输入登录密码" />
		
		<view v-else class="flex-ju-b posire" style="margin-top: 50rpx;">
			<input v-model="code"  placeholder-style="font-size: 26rpx;" class="height100 width1000 bgEDEDED"
				style="border-radius: 30px;text-align: left;text-indent: 15px;"
				placeholder="请输入验证码" />
			<view v-if="butdisabled" style="position: absolute;right: 15px;font-size: 24rpx;color:#24a555;">
				{{codetxt}} s
			</view>
			<view v-else @click="yanzhengma"
				style="position: absolute;right: 15px;font-size: 24rpx;color:#24a555;z-index: 11;">发送验证码</view>
		</view>
		
		
		
		<view class="flex-ju-b color072C5F mt30 size26">
			<view @click="yanzma">{{text}}</view>
			<view>忘记密码？</view>
		</view>
		
		
		<view @click="denglu" :class="islu?'active':''" class="bgF5F5F5 mt50 flex-ju-c height100 ra100 bold colorccc">登录</view>
		<view class=" flex-a-i size26 color072C5F pt30">
			<radio @click="tapradio" color="#27a752" :checked="checked" style="transform:scale(0.7)" borderColor="#27a752" />
			<text @click="tapradio" class="color999">我已阅读并同意</text><text @click="navto('/pages/agreement?guanyu=1')">《用户协议》</text><text @click="navto('/pages/agreement?yinsi=1')">《隐私政策》</text>
		</view>
		
		
		<!-- <view @click="navto('register')">注册</view> -->
		
	</view>
</template>

<script>
	import {
	  mutations as uniIdMutations
	} from '@/uni_modules/uni-id-pages/common/store.js';
 const uniIdCo = uniCloud.importObject("uni-id-co", {customUI: true})
	export default {
		data() {
			return {
				texttitle:"密码登录",
				text:"验证码登录",
				checked:true,
				cid:"",
				account: "18000000003",
				password: "123456",
				butdisabled: false,
				codetxt: 60,
				timer: "",
				code:""
			}
		},
		computed:{
			islu(){
				return this.account&&this.password || this.account&&this.code
			}
		},
		methods: {
			yanzhengma() {
				
				this.http('/api/verify_code').then(res=>{
					if (!this.account) {
						uni.showToast({
							title: "请输入手机号",
							icon: "none"
						})
						return
					}
					if (this.account.trim().length != 11) {
						uni.showToast({
							title: "手机号错误",
							icon: "none"
						})
						return
					}
					this.http('/api/register/verify', {
						phone: this.account,
						type: 'mobile',
						key: res.data.key
					}, 'post').then(res => {
						uni.showToast({
							title: "发送成功",
							icon: "none"
						})
						this.butdisabled = true
						this.codetxt = 60
						this.timer = setInterval(() => {
							this.codetxt--
							if (this.codetxt < 1) {
								this.codetxt = 1
								clearInterval(this.timer)
								this.butdisabled = false
							}
						}, 1000)
					})
				})
				
				
				 
				
			},
			yanzma(){
				
				if(this.text == '验证码登录'){
					this.text = '密码登录'
					this.texttitle = '验证码登录'
					this.password = ''
				}else{
					this.text = '验证码登录'
					this.texttitle = '密码登录'
				}
				
			},
			alogin(resc){
				
				
				if(resc.data.is_reg==1){
					this.imlognin(resc)
				}else{
					this.denglo(resc)
				}
				
				// uni.reLaunch({
				// 	url: '/pages/index/index'
				// })
				
			},
			tapradio(){
				this.checked = !this.checked
			},
			denglo(resc){
				this.http('/api/get_auth_secret', {
					account: this.account,
					password: 'fciu123456',
					type:1,
					nickname:'p'+this.account,
					externalUid: resc.data.user.uid + '',
				}, 'post','isload').then(res => {
					uni.request({
						url: 'https://env-00jxhkl4b8qa.dev-hz.cloudbasefunction.cn/uni-id-co/externalLogin',
						data: {
							clientInfo: uni.getSystemInfoSync(),
							"uniIdToken": "",
							params: {
								nickname:'p'+this.account,
								externalUid: resc.data.user.uid + '',
								account: this.account,
								password: 'fciu123456',
								type: 1
							}
						},
						method: 'POST',
						header: {
							'Cache-Control': 'no-cache',
							'uni-id-nonce': res.data.nonce,
							'uni-id-timestamp': res.data.timestamp,
							'uni-id-signature': res.data.signature,
							'content-Type': 'application/json'
						},
						success: async (res) => {
							
							uni.setStorageSync('token', resc.data.token)
							uni.setStorageSync('phone', this.account)
							uni.setStorageSync('user', resc.data.user)
							
							var uniIdToken = res.data.newToken
							uni.setStorageSync('uni_id_token_expired', uniIdToken.tokenExpired)
							uni.setStorageSync('uni_id_token', uniIdToken.token)
							
							
							this.http('/api/user/edit',{
								uni_uid:res.data.uid
							},'post').then(ress=>{
								
							})
							
							uni.getPushClientId({
							  success: async function(e) {
								let pushClientId = e.cid
								let res = await uniIdCo.setPushCid({
								  pushClientId
								})
							  },
							  fail(e) {
								console.log(e)
							  }
							})
							
							await uniIdMutations.updateUserInfo()
															
							uni.$emit('uni-id-pages-login-success')
							
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					})
				})
			},
			imlognin(resc){
				this.http('/api/get_auth_secret', {
					account: this.account,
					password: 'fciu123456',
					type:2,
					nickname:'p'+this.account,
					externalUid: resc.data.user.uid + '',
				}, 'post','isload').then(res => {
					uni.request({
						url: 'https://env-00jxhkl4b8qa.dev-hz.cloudbasefunction.cn/uni-id-co/externalRegister',
						data: {
							clientInfo: uni.getSystemInfoSync(),
							"uniIdToken": "",
							params: {
								nickname:'p'+this.account,
								externalUid: resc.data.user.uid + '',
								account: this.account,
								password: 'fciu123456',
								type: 2
							}
						},
						method: 'POST',
						header: {
							'Cache-Control': 'no-cache',
							'uni-id-nonce': res.data.nonce,
							'uni-id-timestamp': res.data.timestamp,
							'uni-id-signature': res.data.signature,
							'content-Type': 'application/json'
						},
						success: (res) => {
							this.denglo(resc)
						}
					})
				})
			},
			denglu() {
				
				if(!this.islu){
					return
				}
				if(!this.checked){
					uni.showToast({
						title: "同意协议",
						icon: "none"
					})
					return
				}
				uni.showLoading()
				if(this.text == '密码登录'){
					this.http('/api/login/mobile', {
						phone:this.account,
						captcha:this.code
					},'post','isload').then(resc => {
						this.alogin(resc)
					})
				}else{
					this.http('/api/login', {
						account: this.account,
						password: this.password,
					}, 'post','isload').then(resc => {
						this.alogin(resc)
					})
				}
				
				
			}
		}
	}
</script>

<style>
.active{
	background: linear-gradient(90deg, #27A752, #C6FF00);
	color: #333333;
}
</style>